package com.atguigu.yygh.hosp.testmongo;

import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.regex.Pattern;

@Api(tags = "mongoTemplate操作")
@RestController
@RequestMapping("/mongo")
public class MongoTemplateController {
    @Autowired
    private MongoTemplate mongoTemplate;

    //添加
    @ApiOperation("添加")
    @GetMapping("create")
    public void create(){
        User user = new User();
        user.setName("柳岩");
        user.setAge(25);
        user.setEmail("11211@");
        User user1 = mongoTemplate.insert(user);
        System.out.println("user1 = " + user1);
    }

    //查询所有数据
    @ApiOperation("查询所有")
    @GetMapping("findAll")
    public void findAll(){
        List<User> users = mongoTemplate.findAll(User.class);
        System.out.println("users = " + users);
    }
    //根据id查询
    @ApiOperation("id查询")
    @GetMapping("findId")
    public void findId(){
        User user = mongoTemplate.findById("612f5ea4ac380f518b4da608", User.class);
        System.out.println("user = " + user);
    }
    //条件查询
    @ApiOperation("条件查询")
    @GetMapping("findQuery")
    public void findQuery(){
        Query query = new Query(Criteria.where("name").is("柳岩").and("age").is(25));
        List<User> users = mongoTemplate.find(query, User.class);
        System.out.println("users = " + users);
    }
    //删除
    @ApiOperation("删除")
    @GetMapping("delete")
    public void delete(){
        Query query = new Query(Criteria.where("_id").is("612f5ea4ac380f518b4da608"));
        DeleteResult result = mongoTemplate.remove(query, User.class);
        long deletedCount = result.getDeletedCount();
        System.out.println("deletedCount = " + deletedCount);
    }
    //模糊查询
    @ApiOperation("模糊查询")
    @GetMapping("findLike")
    public void findLike(){
        String name ="岩";
        String regex = String.format("%s%s%s", "^.*", name, ".*$");
       Pattern pattern =Pattern.compile(regex,Pattern.CASE_INSENSITIVE);
        //使用Query对象封装条件   where name like  %est%
        Query query = new Query(Criteria.where("name").regex(pattern));
        List<User> users = mongoTemplate.find(query, User.class);
        System.out.println("users = " + users);

    }
    //分页查询
    @ApiOperation("分页查询")
    @GetMapping("findPage")
    public void findPage(){
        String name ="岩";
        int pageNo =1;
        int pageSize=2;
        String regex = String.format("%s%s%s", "^.*", name, ".*$");
        Pattern pattern = Pattern.compile(regex,Pattern.CASE_INSENSITIVE);
        Query query = new Query(Criteria.where("name").regex(pattern));
        //查询数据总个数
        long count = mongoTemplate.count(query, User.class);
        //查询数据集合
        List<User> users = mongoTemplate.find(query.skip((pageNo - 1) * pageSize).limit(pageSize), User.class);
        System.out.println(count);
        System.out.println(users);
    }
    //修改
    @ApiOperation("修改")
    @GetMapping("update")
    public void update(){
        //先根据id查对象
        User user = mongoTemplate.findById("612f5ea4ac380f518b4da608", User.class);
        //修改设置值 封装到update对象中
        user.setName("花无缺");
        user.setAge(15);
        user.setEmail("135");
        Update update = new Update();
        update.set("name",user.getName());
        update.set("age",user.getAge());
        update.set("email",user.getEmail());
        //把id值封装进条件对象query中
        Query query = new Query(Criteria.where("_id").is(user.getId()));
        UpdateResult upsert = mongoTemplate.upsert(query, update, User.class);
        long modifiedCount = upsert.getModifiedCount();
        System.out.println("modifiedCount = " + modifiedCount);
    }

}
